﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using churchtool.Plugin.Interfaces.ViewModel;
using churchtool.Data;
using churchtool.Operations.Commands;
using churchtool.View;

namespace churchtool.ViewModel
{
    public class SettingsWindowViewModel : ViewModelBase, IPageViewModel
    {

        private new List<TabItem> SettingTabItems = new List<TabItem>();

        #region Constructor

        /// <summary>
        /// Default constructor.
        /// </summary>
        public SettingsWindowViewModel()
        {
            SettingTabItems.Add(new GlobalSettingsTabView());
            //SettingTabItems.Add(new SermonUploadSettingsTabView());

            this.Initialize();
        }

        #endregion

        #region Command Properties

        public ICommand SaveSettings { get; set; }
        public ICommand LoadPlugin { get; set; }

        #endregion

        #region Data Properties

        public string Name
        {
            get { return "Einstellungen"; }
        }

        public List<TabItem> SettingTabs
        {
            get { return SettingTabItems; }
        }

        public TabItem SelectedTab
        {
            get { return SettingTabItems[0]; }
        }

        public CollectionView Languages { get; private set; }

        public string DefaultFilePath
        {
            get
            {
                return GlobalSettings.GetInstance().DefaultFilePath;
            }

            set
            {
                base.RaisePropertyChangingEvent("DefaultFilePath");
                GlobalSettings.GetInstance().DefaultFilePath = value;
                base.RaisePropertyChangedEvent("DefaultFilePath");
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Initializes the view model.
        /// </summary>
        private void Initialize()
        {
            IList<CultureInfo> langs = new List<CultureInfo>();
            langs.Add(new CultureInfo("de-DE"));
            langs.Add(new CultureInfo("en-US"));

            Languages = new CollectionView(langs);
            Languages.MoveCurrentTo(langs[0]);
            Languages.CurrentChanged += new EventHandler(languages_CurrentChanged);

            this.SaveSettings = new SaveSettingsCommand(this);
            this.LoadPlugin = new LoadPluginCommand(this);
        }

        void languages_CurrentChanged(object sender, EventArgs e)
        {
            CultureInfo lang = (CultureInfo)Languages.CurrentItem;
            Thread.CurrentThread.CurrentCulture = lang;
            Thread.CurrentThread.CurrentUICulture = lang;
            App.Current.Resources.MergedDictionaries.Add(App.GetLanguageDictionary(lang));
            GlobalSettings.GetInstance().Language = lang.ToString();
        }

        #endregion

        #region Methods

        #endregion
    }
}
